package com.openbravo.editor;

import com.openbravo.basic.BasicException;
import com.openbravo.format.DoubleUtils;
import com.openbravo.format.Formats;
import java.awt.Toolkit;

public abstract class JEditorNumber extends JEditorAbstract
{
  private static final int NUMBER_ZERONULL = 0;
  private static final int NUMBER_INT = 1;
  private static final int NUMBER_DEC = 2;
  private int m_iNumberStatus;
  private String m_sNumber;
  private boolean m_bNegative;
  private Formats m_fmt = getFormat();

  public JEditorNumber()
  {
    reset();
  }

  protected abstract Formats getFormat();

  public void reset()
  {
    String str = getText();
    this.m_sNumber = "";
    this.m_bNegative = false;
    this.m_iNumberStatus = 0;
    reprintText();
    firePropertyChange("Text", str, getText());
  }

  public void setDoubleValue(Double paramDouble)
  {
    String str = getText();
    if (paramDouble == null)
    {
      this.m_sNumber = "";
      this.m_bNegative = false;
      this.m_iNumberStatus = 0;
    }
    else if (paramDouble.doubleValue() >= 0.0D)
    {
      this.m_sNumber = formatDouble(paramDouble);
      this.m_bNegative = false;
      this.m_iNumberStatus = 0;
    }
    else
    {
      this.m_sNumber = formatDouble(Double.valueOf(-paramDouble.doubleValue()));
      this.m_bNegative = true;
      this.m_iNumberStatus = 0;
    }
    reprintText();
    firePropertyChange("Text", str, getText());
  }

  public Double getDoubleValue()
  {
    String str = getText();
    if ((str == null) || (str.equals("")))
      return null;
    try
    {
      return Double.valueOf(Double.parseDouble(str));
    }
    catch (NumberFormatException localNumberFormatException)
    {
    }
    return null;
  }

  public void setValueInteger(int paramInt)
  {
    String str = getText();
    if (paramInt >= 0)
    {
      this.m_sNumber = Integer.toString(paramInt);
      this.m_bNegative = false;
      this.m_iNumberStatus = 0;
    }
    else
    {
      this.m_sNumber = Integer.toString(-paramInt);
      this.m_bNegative = true;
      this.m_iNumberStatus = 0;
    }
    reprintText();
    firePropertyChange("Text", str, getText());
  }

  public int getValueInteger()
    throws BasicException
  {
    try
    {
      return Integer.parseInt(getText());
    }
    catch (NumberFormatException localNumberFormatException)
    {throw new BasicException(localNumberFormatException);
    }
    
  }

  private String formatDouble(Double paramDouble)
  {
    String str = Double.toString(DoubleUtils.fixDecimals(paramDouble));
    if (str.endsWith(".0"))
      str = str.substring(0, str.length() - 2);
    return str;
  }

  protected String getEditMode()
  {
    return "-1.23";
  }

  public String getText()
  {
    return new StringBuilder().append(this.m_bNegative ? "-" : "").append(this.m_sNumber).toString();
  }

  protected int getAlignment()
  {
    return 4;
  }

  protected String getTextEdit()
  {
    return getText();
  }

  protected String getTextFormat()
    throws BasicException
  {
    return this.m_fmt.formatValue(getDoubleValue());
  }

  protected void typeCharInternal(char paramChar)
  {
    transChar(paramChar);
  }

  protected void transCharInternal(char paramChar)
  {
    String str = getText();
    if (paramChar == '')
    {
      reset();
    }
    else if (paramChar == '-')
    {
      this.m_bNegative = (!this.m_bNegative);
    }
    else if ((paramChar == '0') && (this.m_iNumberStatus == 0))
    {
      this.m_sNumber = "0";
    }
    else if (((paramChar == '1') || (paramChar == '2') || (paramChar == '3') || (paramChar == '4') || (paramChar == '5') || (paramChar == '6') || (paramChar == '7') || (paramChar == '8') || (paramChar == '9')) && (this.m_iNumberStatus == 0))
    {
      this.m_iNumberStatus = 1;
      this.m_sNumber = Character.toString(paramChar);
    }
    else if ((paramChar == '.') && (this.m_iNumberStatus == 0))
    {
      this.m_iNumberStatus = 2;
      this.m_sNumber = "0.";
    }
    else if (((paramChar == '0') || (paramChar == '1') || (paramChar == '2') || (paramChar == '3') || (paramChar == '4') || (paramChar == '5') || (paramChar == '6') || (paramChar == '7') || (paramChar == '8') || (paramChar == '9')) && (this.m_iNumberStatus == 1))
    {
      this.m_sNumber = new StringBuilder().append(this.m_sNumber).append(paramChar).toString();
    }
    else if ((paramChar == '.') && (this.m_iNumberStatus == 1))
    {
      this.m_iNumberStatus = 2;
      this.m_sNumber = new StringBuilder().append(this.m_sNumber).append('.').toString();
    }
    else if (((paramChar == '0') || (paramChar == '1') || (paramChar == '2') || (paramChar == '3') || (paramChar == '4') || (paramChar == '5') || (paramChar == '6') || (paramChar == '7') || (paramChar == '8') || (paramChar == '9')) && (this.m_iNumberStatus == 2))
    {
      this.m_sNumber = new StringBuilder().append(this.m_sNumber).append(paramChar).toString();
    }
    else
    {
      Toolkit.getDefaultToolkit().beep();
    }
    firePropertyChange("Text", str, getText());
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.editor.JEditorNumber
 * JD-Core Version:    0.6.0
 */